package com.xzj.admin.analysis.base.custom.ct;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.xzj.admin.analysis.base.common.handle.ThemeSpecialAnalysisHandle;
import com.xzj.admin.analysis.base.custom.common.PlatformSpecialHandle;
import com.xzj.admin.analysis.base.domain.ResMessageManage;
import com.xzj.admin.analysis.base.domain.CommonFlowInfo;
import lombok.extern.slf4j.Slf4j;

/**
 * @program: xzj-admin
 * @ClassName: NmThemeSpecialAnalysisHandle
 * @description: 内蒙特殊主题处理业务
 * @author: XiaZunJun
 * @create: 2024-11-18 16:29
 */
@Slf4j
public class NmThemeSpecialAnalysisHandle extends ThemeSpecialAnalysisHandle {

    /**
     * 宿主机 id与名称映射关系标签
     */
    private static final String HOST_ID_NAME_RELATE = "hostIdName";

    /**
     * 宿主机 名称与id映射关系标签
     */
    private static final String HOST_NAME_ID_RELATE = "hostNameId";

    public NmThemeSpecialAnalysisHandle(String cloudId, String cloudType) {
        super(cloudId, cloudType, message ->
                RESOURCE_TYPE.host.equals(message.getMessageSource())
                        ||RESOURCE_TYPE.flavors.equals(message.getMessageSource())
                        || RESOURCE_TYPE.vm.equals(message.getMessageSource())
                        || RESOURCE_TYPE.datacenter.equals(message.getMessageSource())
                        || RESOURCE_TYPE.cluster.equals(message.getMessageSource())
                        || RESOURCE_TYPE.region.equals(message.getMessageSource())
                        || "clusterHostRelation".equals(message.getMessageSource())
        );
    }

    static {
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.host.name(), MESSAGE_TAG.add.name()),NmThemeSpecialAnalysisHandle::hostAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.host.name(), MESSAGE_TAG.edit.name()),NmThemeSpecialAnalysisHandle::hostEdit);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.host.name(), MESSAGE_TAG.del.name()),NmThemeSpecialAnalysisHandle::hostDel);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.flavors.name(), MESSAGE_TAG.add.name()),NmThemeSpecialAnalysisHandle::flavorsAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.flavors.name(), MESSAGE_TAG.edit.name()),NmThemeSpecialAnalysisHandle::flavorsAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.flavors.name(), MESSAGE_TAG.del.name()),NmThemeSpecialAnalysisHandle::flavorsDel);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.vm.name(), MESSAGE_TAG.add.name()),NmThemeSpecialAnalysisHandle::vmAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.vm.name(), MESSAGE_TAG.edit.name()),NmThemeSpecialAnalysisHandle::vmAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.datacenter.name(), MESSAGE_TAG.add.name()),NmThemeSpecialAnalysisHandle::datacenterAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.datacenter.name(), MESSAGE_TAG.edit.name()),NmThemeSpecialAnalysisHandle::datacenterAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.cluster.name(), MESSAGE_TAG.add.name()),NmThemeSpecialAnalysisHandle::clusterAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.cluster.name(), MESSAGE_TAG.edit.name()),NmThemeSpecialAnalysisHandle::clusterAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.region.name(), MESSAGE_TAG.add.name()),PlatformSpecialHandle::regionAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.region.name(), MESSAGE_TAG.edit.name()),PlatformSpecialHandle::regionAdd);
        putThemeSpecialHandle(buildKey("clusterHostRelation", MESSAGE_TAG.add.name()),NmThemeSpecialAnalysisHandle::clusterHostRelationAdd);
        putThemeSpecialHandle(buildKey("clusterHostRelation", MESSAGE_TAG.edit.name()),NmThemeSpecialAnalysisHandle::clusterHostRelationAdd);
    }

    private static void hostAdd(ResMessageManage message, CommonFlowInfo info){
        JSONObject hostJson = JSONObject.parseObject(message.getMessageJson());
        String hostName = hostJson.getString("hypervisor_hostname");
        redisHandle.setCacheMapValue(buildKey(info.getCacheKeyPrefix(),HOST_ID_NAME_RELATE),message.getMessageId(), hostName);
        redisHandle.setCacheMapValue(buildKey(info.getCacheKeyPrefix(),HOST_NAME_ID_RELATE),hostName, message.getMessageId());
        PlatformSpecialHandle.vmOrHostAdd(message,info);
    }
    private static void hostEdit(ResMessageManage message, CommonFlowInfo info){
        JSONObject hostJson = JSONObject.parseObject(message.getMessageJson());
        String hostName = hostJson.getString("hypervisor_hostname");
        String cacheName = redisHandle.getCacheMapValue(buildKey(info.getCacheKeyPrefix(),HOST_ID_NAME_RELATE),message.getMessageId());
        if(!hostName.equals(cacheName)){
            redisHandle.deleteHashObject(buildKey(info.getCacheKeyPrefix(),HOST_NAME_ID_RELATE),cacheName);
            redisHandle.setCacheMapValue(buildKey(info.getCacheKeyPrefix(),HOST_NAME_ID_RELATE),hostName,message.getMessageId());
            redisHandle.setCacheMapValue(buildKey(info.getCacheKeyPrefix(),HOST_ID_NAME_RELATE),message.getMessageId(), hostName);
        }
        PlatformSpecialHandle.vmOrHostAdd(message,info);
    }

    private static void hostDel(ResMessageManage message, CommonFlowInfo info){
        String cacheName = redisHandle.getCacheMapValue(buildKey(info.getCacheKeyPrefix(),HOST_ID_NAME_RELATE),message.getMessageId());
        redisHandle.deleteHashObject(buildKey(info.getCacheKeyPrefix(),HOST_NAME_ID_RELATE),cacheName);
        redisHandle.deleteHashObject(buildKey(info.getCacheKeyPrefix(),HOST_ID_NAME_RELATE),message.getMessageId());
    }

    private static void flavorsAdd(ResMessageManage message, CommonFlowInfo info){
        JSONObject row = message.getRowData();
        JSONObject flavor = new JSONObject();
        flavor.put("ram",row.getString("ram"));
        flavor.put("disk",row.getString("disk"));
        flavor.put("vcpus",row.getString("vcpus"));
        String key = buildKey(info.getCloudType(),info.getRmPoolId(),"flavor");
        redisHandle.setCacheMapValue(key,message.getMessageId(),flavor.toJSONString());
    }

    private static void flavorsDel(ResMessageManage message, CommonFlowInfo info){
        String key = buildKey(info.getCloudType(),info.getRmPoolId(),"flavor");
        redisHandle.deleteHashObject(key,message.getMessageId());
    }

    private static void vmAdd(ResMessageManage message, CommonFlowInfo info){
        JSONObject row = message.getRowData();
        JSONObject flavor = row.getJSONObject("flavor");
        if(null != flavor){
            String flavorId = flavor.getString("id");
            if(StrUtil.isNotEmpty(flavorId)){
                String key = buildKey(info.getCloudType(),info.getRmPoolId(),"flavor");
                String cacheMapValue = redisHandle.getCacheMapValue(key, flavorId);
                if(StrUtil.isNotEmpty(cacheMapValue)){
                    JSONObject sourceFlavor = JSONObject.parseObject(cacheMapValue);
                    for(String flavorKey:sourceFlavor.keySet()){
                        flavor.put(flavorKey,sourceFlavor.getString(flavorKey));
                    }
                    row.put("flavor",flavor);
                }
            }
        }
        PlatformSpecialHandle.vmOrHostAdd(message,info);
    }


    private static void datacenterAdd(ResMessageManage message, CommonFlowInfo info) {
        String regionDatacenter = buildKey(info.getRmPoolId(), info.getStandardCode());
        redisHandle.setCacheObject(regionDatacenter, message.getMessageId());
        PlatformSpecialHandle.datacenterAdd(message, info);
    }

    private static void clusterAdd(ResMessageManage message, CommonFlowInfo info){
        String regionDatacenter = buildKey(info.getRmPoolId(), info.getStandardCode());
        String datacenterId = redisHandle.getCacheObject(regionDatacenter);
        message.getRowData().put("availablePartId", datacenterId);
        message.setMessageJson(message.getRowData().toJSONString());
    }

    private static void clusterHostRelationAdd(ResMessageManage message, CommonFlowInfo info){
        JSONObject row = message.getRowData();
        String hostName = row.getString("host_name");
        String hostId = redisHandle.getCacheMapValue(buildKey(info.getCacheKeyPrefix(),HOST_NAME_ID_RELATE),hostName);
        if(null != hostId){
            row.remove("service");
            message.setMessageJson(message.getRowData().toJSONString());
        }else{
            message.setMessageId(null);
        }

    }
}
